import {
    querythelistofmassmessagers,
    updatepitchon,
    updatepitchons,
    gethistorymessagedetailapi
} from '../../api/message'
import {
    getCardAge,
    getGenderByIdNumber
} from '../../../utils/util'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        showsearchipt: false,
        sendNumber: 0,
        id: 0,
        maxnum: 0,
        total: 0,
        personlist: [],
        pageNum: 1,
        pageSize: 20,
        changeidarr: [],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        this.setData({
            id: options.id
        })
        let res = await querythelistofmassmessagers({
            massMessageId: options.id,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        console.log(res);
        let list = res.rows
        list.forEach((ele, index) => {
            list[index].age = getCardAge(ele.idCardNo)
            list[index].sex = getGenderByIdNumber(ele.idCardNo)
        })
        let maxnum = Math.ceil(res.total / 20)
        this.setData({
            personlist: list,
            total: res.total,
            maxnum: maxnum
        })

        let res2 = await gethistorymessagedetailapi(this.data.id)
        console.log(res2, '111111111');
        this.setData({
            sendNumber: res2.data.sendNumber
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    changeselectstatus(e) {
        console.log(e, 'event ')
        this.data.personlist[e.currentTarget.dataset.i].selectStatus = e.currentTarget.dataset.status === 1 ? 0 : 1
        if (e.currentTarget.dataset.status === 1) {
            this.setData({
                sendNumber: this.data.sendNumber - 1
            })
        } else {
            this.setData({
                sendNumber: this.data.sendNumber + 1
            })
        }
        var i = this.data.changeidarr.findIndex((ele, index) => {
            return ele === e.currentTarget.dataset.id
        })
        if (i === -1) {
            this.data.changeidarr.push(e.currentTarget.dataset.id)
        } else {
            this.data.changeidarr.splice(i, 1)
        }
        this.setData({
            personlist: this.data.personlist
        })
    },
    async updatepitchon(selectStatus, id) {
        let res = await updatepitchon({
            massMessageId: this.data.id,
            id: id,
            selectStatus: selectStatus
        })
        console.log(res);
        if (res.code == 200) {
            let res = await gethistorymessagedetailapi(this.data.id)
            console.log(res, '111111111');
            this.setData({
                sendNumber: res.data.sendNumber
            })
        }
    },
    async cancelshowipt() {
        let res2 = await gethistorymessagedetailapi(this.data.id)
        console.log(res2, '111111111');
        this.setData({
            sendNumber: res2.data.sendNumber,
            pageNum: 1,
            showsearchipt: false
        })
        let res = await querythelistofmassmessagers({
            massMessageId: this.data.id,
            pageNum: 1,
            pageSize: this.data.pageSize
        })
        console.log(res);
        let list = res.rows
        list.forEach((ele, index) => {
            list[index].age = getCardAge(ele.idCardNo)
            list[index].sex = getGenderByIdNumber(ele.idCardNo)
            this.data.changeidarr.forEach((item, i) => {
                if (ele.id == item) {
                    list[index].selectStatus = 0
                }
            })
        })
        let maxnum = Math.ceil(res.total / 20)
        this.setData({
            personlist: list,
            total: res.total,
            maxnum: maxnum,
            sendNumber: this.data.total - this.data.changeidarr.length
        })
    },
    search() {
        this.setData({
            name: '',
            showsearchipt: true
        })
    },
    async chooseok() {
        if (this.data.changeidarr.length != 0) {
            // 调用批量更新
            let res = await updatepitchons({
                massMessageId: this.data.id,
                ids: this.data.changeidarr
            })
            console.log(res, '99999999999999999999999');
        }
        wx.navigateBack()
        // wx.navigateTo({
        //     url: '../../pages/new-group-message/new-group-message?statue=' + true + '&id=' + this.data.id,
        // })
    },
    changesearchname(e) {
        this.setData({
            name: e.detail.value
        })
    },
    async iptok(e) {
        console.log(e);
        let res = await querythelistofmassmessagers({
            sendUserName: this.data.name,
            massMessageId: this.data.id,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        console.log(res);
        let list = res.rows
        list.forEach((ele, index) => {
            list[index].age = getCardAge(ele.idCardNo)
            list[index].sex = getGenderByIdNumber(ele.idCardNo)
            this.data.changeidarr.forEach((item, i) => {
                if (ele.id == item) {
                    list[index].selectStatus = 0
                }
            })
        })
        let maxnum = Math.ceil(res.total / 20)
        this.setData({
            personlist: list,
            maxnum: maxnum,
            sendNumber: this.data.total - this.data.changeidarr.length
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },
    // 到达底部了
    async onScrolltolower(){
        console.log('到达底部了')
        if (this.data.pageNum < this.data.maxnum) {
            let pagnum = this.data.pageNum + 1
            this.setData({
                pageNum: pagnum
            })
            let res = await querythelistofmassmessagers({
                sendUserName: this.data.name,
                massMessageId: this.data.id,
                pageNum: this.data.pageNum,
                pageSize: this.data.pageSize
            })
            console.log(res);
            let list = res.rows
            list.forEach((ele, index) => {
                list[index].age = getCardAge(ele.idCardNo)
                list[index].sex = getGenderByIdNumber(ele.idCardNo)
            })
            let clist = this.data.personlist.concat(list)
            this.setData({
                personlist: clist
            })
        }
        // else {
        //     wx.showToast({
        //         title: '暂无更多数据',
        //         icon: "none"
        //     })
        //     return
        // }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    async onReachBottom() {
        console.log('触发了吗')
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})